<template>
	<view>
		<view class="container-box">
			<index @fresh="fresh" ref="index" @ShowNews="ShowNews" v-if="PageCur=='index'"></index>
			<message @fresh="fresh" ref="notice" v-if="PageCur=='notice'"></message>
			<friend @fresh="fresh" @menu="menu" ref="friend" v-if="PageCur=='friend'"></friend>
			<publish v-if="PageCur=='publish'"></publish>
			<more v-if="PageCur=='more'"></more>
			<me @menu="menu" ref="me" v-if="PageCur=='me'"></me>
		</view>

		<f-tabbar></f-tabbar>
		<u-action-sheet :list="sheetList" v-model="sheetShow" @click="clickSheet"></u-action-sheet>
	</view>
</template>

<script>
	import index from "./user/inedx.vue";	//首页
	import message from "./message.vue";	//站内信息
	import more from "./more.vue";	//更多
	import friend from "./world/index.vue"; // 圈子
	import me from "@/pages/me/index.vue";	//个人中心
	import fTabbar from '@/components/f-tabbar/f-tabbar';
	export default {
		components: {
			index,
			friend,
			more,
			message,
			me,
			fTabbar
		},
		data() {
			return {
				PageCur: 'friend',
				openId:'',
				access_token:'',
				tip:"我是提示",
				duration:1,
				sheetList: [],
				sheetShow: false,
			};
		},
		// 分享小程序
		onShareAppMessage(res) {
			return {
				title: '送你一个对象，快来「处CP」吧！',
				imageUrl: 'http://rbtnet.oss-cn-hangzhou.aliyuncs.com/test/20220914/58242695c2a8421b892eced5f1bd3b15.jpeg',
			};
		},
		onShareTimeline() {
			return {
				title: '送你一个对象，快来「处CP」吧！',
				imageUrl: 'http://rbtnet.oss-cn-hangzhou.aliyuncs.com/test/20220914/58242695c2a8421b892eced5f1bd3b15.jpeg',
			}
		},
		onReachBottom() {
			switch(this.PageCur) {
			     case 'index':
			        //this.$refs.index.load();
			        break;
			     case 'notice':
			        this.$refs.notice.load();
			        break;
			     default:
			} 
			console.log('到底');
		},
		onPullDownRefresh() {
			switch(this.PageCur) {
			     case 'index':
			        this.$refs.index.reload();
			        break;
				 case 'friend':
					this.$refs.friend.reload();
					break;
			     case 'notice':
			        this.$refs.notice.reload();
			        break;
			     default:
					uni.stopPullDownRefresh();
			} 
		},
		onLoad(option) {
			this.checkVersion();
		},
		onPageScroll(e) {
			switch(this.PageCur) {
			     case 'index':
			        this.$refs.index.onScrollTop(e);
			        break;
			     case 'notice':
			        this.$refs.notice.onScrollTop(e);
			        break;
			     default:
			} 
		},
		onShow() {
			this.$u.get('/system/config/getChatValue',{
				appId: this.website.appId,
				userId: this.vuex_user.userId,
			}).then(res => {
				this.$u.vuex('chat_flag', res.data);
			});
			//this.getWebsocket();
			//this.getUnRead();
			this.$nextTick(()=>{
				switch(this.PageCur) {
					 case 'index':
						//this.$refs.index.query();
						break;
					 case 'friend':
						//this.$refs.friend.openTab(tip);
						break;
					 case 'notice':
						this.$refs.notice.query();
						break;
					 default:
				} 
			});
		},
		methods: {
			checkVersion() {
				if (uni.getUpdateManager) {
					const updateManager = uni.getUpdateManager();
					updateManager.onUpdateReady(function(res) {
					    uni.showModal({
					        title: "更新提示",
					        content: "检测到新版本，是否下载新版本并重启小程序？",
					        success(res) {
					            if (res.confirm) {
					                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
					                updateManager.applyUpdate();
					            }
					        }
					    });
					});
					updateManager.onUpdateFailed(function(res) {
					    // 新的版本下载失败
					    uni.showModal({
					        title: "已经有新版本了哟~",
					        content: "新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~",
					        showCancel: false
					    });
					});
				}
			},
			publish() {
				this.$u.route({
					url: 'pages/me/publish',
				});
			},
			ShowNews(e){
				console.log(e)
				this.PageCur = e;
			},
			NavChange: function(e) {
				this.PageCur = e.currentTarget.dataset.cur;
			},
			NavChange_xd: function() {
				uni.navigateTo({
					url: 'publish',
					animationType: 'slide-in-bottom',
					animationDuration: 200
				});
			},
			getWebsocket() {
			    uni.closeSocket();
			    uni.connectSocket({
			        url: this.website.wsUrl+"/im/websocket?token="+uni.getStorageSync("token")
			    });
				var that = this;
			    uni.onSocketMessage(function(res) {
					if(that.PageCur == 'notice'){
						that.$refs.notice.reload();
					}
					that.getUnRead();
			    });
			},
			getUnRead() {
				this.$u.get('/im/roomUser/getUnRead').then(res => {
					this.$u.vuex('message', res.data);
				})
			},
			fresh() {
				uni.stopPullDownRefresh();
			},
			menu(e) {
				this.sheetList = e;
				this.sheetShow = true;
			},
			clickSheet(index) {
				var sheet = this.sheetList[index];
				switch(this.PageCur) {
					 case 'friend':
						this.$refs.friend.clickSheet(sheet);
						break;
				     case 'me':
				        this.$refs.me.clickSheet(sheet);
				        break;
				     default:
				} 
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #fff;
	}
	.box {
		margin: 20upx 0;
	}
	.box view.cu-bar {
		margin-top: 20upx;
	}
	
	.logo_btn{
		width: 38*2rpx;
		height: 38*2rpx;
		position: absolute;
		z-index: 2;
		border-radius: 50%;
		top: -40rpx;
		left: 0rpx;
		right: 0;
		margin: auto;
		padding: 0;
	}
	.cu-bar.tabbar .action.add-action {
	    padding-top: 56rpx !important;
	}
	.container-box {
		padding-bottom: 100rpx;
	}
</style>
